package com.nwpu.coursesupport.service.imple;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.enumeration.TaskType;
import com.nwpu.coursesupport.mapper.LessonMapper;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.view.LessonTaskView;
import com.nwpu.coursesupport.view.LessonView;
import com.nwpu.coursesupport.view.criteria.LessonTaskViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.LessonTaskViewMapper;

@Service
public class LessonTaskViewService extends AbstractService{

	@Autowired
	private LessonTaskViewMapper lessonTaskViewMapper;
	@Autowired
	private LessonMapper lessonMapper;
	
    protected static Logger logger = LoggerFactory.getLogger(LessonTaskViewService.class);

	
	public JSONResult getTeacherView(LessonTaskViewSearchCriteria sc){
		
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getLessonId()<=0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		LessonView view=this.lessonMapper.findViewById(sc.getLessonId());
		if(view==null){
			logger.error("该课程不存在");
			return rst.fail("该课程不存在");
		}
		List<LessonTaskView> list=this.lessonTaskViewMapper.getTeacherView(sc);
		
		rst.addData("list", list);
		rst.addData("lessonId", sc.getLessonId());
		//rst.addData("lesson",view);
		rst.success();
		return rst;
	}
	
	public JSONResult getStudentView(LessonTaskViewSearchCriteria sc){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getLessonId()<=0){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		LessonView view=this.lessonMapper.findViewById(sc.getLessonId());
		if(view==null){
			logger.error("该课程不存在");
			return rst.fail("该课程不存在");
		}
		sc.setIsVisible(Status.YES);
		int id=(int) getSession().getAttribute("id");
		sc.setStuId(id);
		List<LessonTaskView> results=null;
		if(sc.getType()==null){
			results=new ArrayList<LessonTaskView>();
			sc.setType(null);
			sc.setSubmitType(TaskType.TASKMAIN);
			results.addAll(getTaskMain(sc));
			results.addAll(getTaskInLesson(sc));
			sorted(results);
		}else if(sc.getType()==TaskType.ASKINLESSON){
			results=getTaskInLesson(sc);
		}else{
			results=this.lessonTaskViewMapper.getStudentTaskMainView(sc);
		}
		rst.addData("list", results);
		rst.addData("lesson", view);
		rst.success();
		return rst;
	}
	
	private List<LessonTaskView> getTaskMain(LessonTaskViewSearchCriteria sc){
		List<LessonTaskView> list=this.lessonTaskViewMapper.getStudentTaskMainView(sc);
		return list;
	}
	
	private List<LessonTaskView> getTaskInLesson(LessonTaskViewSearchCriteria sc){
		sc.setType(TaskType.ASKINLESSON);
		List<LessonTaskView> list=this.lessonTaskViewMapper.getStudentTaskInLessonView(sc);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public static void sorted(List<LessonTaskView> list){
		if(list==null){
			return ;
		}
		Collections.sort(list, new Comparator(){
			@Override
			public int compare(Object o1, Object o2) {
				LessonTaskView p1 = (LessonTaskView) o1;   
				LessonTaskView p2 = (LessonTaskView) o2; 
				return p2.getAssignDate().compareTo(p1.getAssignDate());
			}
			
		});
	}
}
